package dbinter

import (
	"fmt"
	related "orm/relatable"

	"github.com/jinzhu/gorm"
)

func FirstFunc(db *gorm.DB) {
	var user related.User1
	db.First(&user, 1)
	fmt.Printf("创建first 信息\n")
	fmt.Println(user)

	var user2 related.User1
	db.Where("Id=?", 2).First(&user2)
	fmt.Printf("创建first2 信息\n")
	fmt.Println(user2)
}

func FirOrCreFunc(db *gorm.DB) {
	var user related.User1
	user2 := &related.User1{Name: "xiaohong", Age: 11, Addr: "hujian"}
	db.FirstOrCreate(&user, user2)
	fmt.Println(user)
}

func LasOrtTake(db *gorm.DB) {
	var user related.User1
	db.Last(&user, 1)
	fmt.Printf("last info %v\n", user)

	var user1 related.User1
	db.Take(&user1, 1)
	fmt.Printf("take info %v\n", user1)

	var user2 related.User1
	db.Find(&user2, 2)
	fmt.Printf("find info %v\n", user2)
}

func WheOrSel(db *gorm.DB) {
	// var user related.User1
	// db.Where("Name=?", "xiaoming").Find(&user)
	// fmt.Printf("where info %v\n", user)

	var user1 related.User1
	db.Select("addr").Where("Name=?", "xiaoming").Find(&user1)
	fmt.Printf("select info %v\n", user1)
}

func CreOrSav(db *gorm.DB) {
	// var user related.User1
	// db.Where("Name=?", "xiaoming").Find(&user)
	// fmt.Printf("where info %v\n", user)

	user1 := related.User1{Name: "xiaopeng", Age: 15, Addr: "xinjiang"}
	db.Create(&user1)
	fmt.Println(user1)

	var user2 related.User1
	db.Where("Name=?", "xiaopeng").First(&user2)
	user2.Name = "xiaoju"
	db.Save(&user2)
	fmt.Println(user2)

}

func DelOrUpd(db *gorm.DB) {
	// var user related.User1
	// db.Where("Name=?", "xiaoming").Find(&user)
	// fmt.Printf("where info %v\n", user)

	var user related.User1
	db.Find(&user, 1).Update("Name", "xiaoshu")
	fmt.Println(user)

	var user1 related.User1
	db.Find(&user1, 1)
	db.Delete(&user1)
	fmt.Println(user1)
}

func NoOrOrder(db *gorm.DB) {
	// var user related.User1
	// db.Where("Name=?", "xiaoming").Find(&user)
	// fmt.Printf("where info %v\n", user)

	var user []related.User1
	db.Where("Name=?", "xiaoming").Order("id desc").Find(&user)
	fmt.Println(user)

}

func OffOrLim(db *gorm.DB) {
	var user related.User1
	db.Limit(2).Offset(3).Find(&user)
	fmt.Println(user)
}

type UserInfo struct {
	Name string
	Age  int
	Addr string
}

func ScanFunc(db *gorm.DB) {
	var userinfo UserInfo
	var user related.User1
	db.Find(&user).Scan(&userinfo)
	fmt.Println(userinfo)
}

type CountData struct {
	Name  string
	Count int
}

func CountOrHaving(db *gorm.DB) {
	var user related.User1
	var countdata []CountData
	db.Select("Name,count(*) as count").Group("id").Find(&user).Scan(&countdata)
	fmt.Println(countdata[1].Count)
	fmt.Println(user)
}

type UserAll struct {
	Id    int
	Pid   int
	Name  string
	Age   int
	Addr  string
	Grade int
	Class int
}

func JoinFunc(db *gorm.DB) {
	var user []related.User1
	var userall []UserAll
	db.Select("related.User1.*, UserAll.*").Joins("left join grade_infos on user1.id=grade_infos.id").Find(&user).Scan(&userall)
	fmt.Println(user)
	fmt.Println(userall)
}

func AssignOrAttrs(db *gorm.DB) {
	var user related.User1
	db.FirstOrInit(&user, related.User1{Name: "xiaosi", Age: 18})
	fmt.Println(user)

	var user1 related.User1
	db.Where("Name = ?", "xiaosi").Assign(related.User1{Name: "xiaojiu"}).FirstOrInit(&user1)
	fmt.Println(user1)
}

func LoadMode(db *gorm.DB) {
	ret := db.Begin()

	var user related.User1
	result := db.FirstOrInit(&user, related.User1{Name: "xiaosi", Age: 18})
	if result.Error != nil {
		ret.Rollback()
	} else {
		ret.Commit()
	}
	fmt.Println(user)

	// var user1 related.User1
	// db.Where("Name = ?", "xiaosi").Assign(related.User1{Name: "xiaojiu"}).FirstOrInit(&user1)
	// fmt.Println(user1)
	// db.LogMode(true)
}

func OriSql(db *gorm.DB) {
	var user []related.User1
	db.Raw("select * from user1 where name = ?", "xiaoming").Find(&user)
	// var user1 related.User1
	// db.Where("Name = ?", "xiaosi").Assign(related.User1{Name: "xiaojiu"}).FirstOrInit(&user1)
	fmt.Println(user)
	// db.LogMode(true)
}
